﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using OpenCRM.DomainServices;
using OpenCRM.DomainServices.DomainEntities;

namespace OpenCRM.Common.Services.AccessSurface.Global
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
       

        #region IService1 Members

        string IService1.GetData(int value)
        {
            throw new NotImplementedException();
        }

        OpenCRM.DomainServices.DomainEntities.Client[] IService1.GetClients()
        {
            string userName;
            string certificateSubjectName;

            GetCallerIdentities(
                OperationContext.Current.ServiceSecurityContext,
                out userName,
                out certificateSubjectName);


            SimplesServices serv = new SimplesServices();
            return serv.GetClientsList(userName).ToArray<Client>();
        }

        private void GetCallerIdentities(ServiceSecurityContext serviceSecurityContext, out string userName, out string certificateSubjectName)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IService1 Members


        public Client GetClient(string idClient)
        {
            string userName = "test";
            long longIdClient;
            //... code qui gère la reconnaissance de l'authenticité du User (ou Tennant)

            //vérifier le parametre qui est forcement string car c'est transmis dans une URI
            if (!long.TryParse(idClient, out longIdClient))
            {
                 WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                return null;
            }

            //appel de notre objet métier fiabilisé
            SimplesServices serv = new SimplesServices();
            List<Client> res = serv.GetClient(longIdClient, userName);

            //verifier que il y a quelque chose dans la liste résultat
            if (res.Count == 1)
                return res[0];
            else
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
                return null;
            }
        }

        #endregion
    }
}
